var orm = require('orm');
// var mongoose = require('mongoose');    //引用mongoose模块
var lyby = require('./util.js');
var fs = require('fs');

var lybyDB = function (options) {
    lyby.apply(this, options);
};
lybyDB.prototype = {
    init: function (dbStr) {
        var me = this;
        var modelPacket = this.models||'models';
        this.app.use(orm.express(dbStr, {
            define: function (db, models, next) {
                console.log(modelPacket+' create begin');
                fs.readdir(__dirname+'/'+modelPacket,function(error,files){
                    files.forEach(function(key){
                        /*key = key.replace('.js','');
                        var model = require("./models/"+key);
                        model(db,function(){});*/
                        if(!(/[.js]$/.test(key)))return;
                        db.load("./"+modelPacket+"/"+key, function (err) {
                            if(err)console.error(err);
                            // loaded!
                            //var Person = db.models.person;
                        });
                    });
                    //lyby.apply(models,db.models);
                    console.log('数据库模型已创建完成');
                    next();
                });
                /*for (var key in me.models) {
                    models[key] = me.models[key](db, key);
                }*/
            }
        }));
    },
    initMongodb:function (db) {
        var modelPacket = this.models||'models';
        fs.readdir(__dirname+'/'+modelPacket+'/mongoose',function(error,files){
            files.forEach(function(key){
                /*key = key.replace('.js','');
                 var model = require("./models/"+key);
                 model(db,function(){});*/
                if(!(/[.js]$/.test(key)))return;
                var modal = require(__dirname+'/'+modelPacket+'/mongoose/'+key)
                modal.init(db);
            });
            //lyby.apply(models,db.models);
            console.log('***mongodb is finished***');
        });
        // mongoose.Schema.Types.ObjectId
        // var PersonSchema = new mongoose.Schema({
        //     name:String   //定义一个属性name，类型为String
        // });
        // var PersonModel = db.model('Person',PersonSchema,"wolaigequ");
//如果该Model已经发布，则可以直接通过名字索引到，如下：
//var PersonModel = db.model('Person');
//如果没有发布，上一段代码将会异常

        // PersonModel.findOne({name:'Krouky'},function (err, item) {
        //     console.log(item)
        // });
        // var personEntity = new PersonModel({name:'Krouky'});
        // personEntity.save();  //执行完成后，数据库就有该数据了

        //为Schema模型追加speak方法
        //personEntity.speak();//我的名字叫Krouky

        //一次打开记录
    }/*,
    models: function () {
        return {
            //大赛列表
            contest: function (db, tbName) {
                return db.define(tbName, {
                    id :Number,
                    title: String,
                    state : Number,
                    recommendation : Number,
                    rating : Number,
                    createdTime: {type: 'date', mapsTo: 'created_time'}
                });
            },
            //用户表
            emlog_user: function (db, tbName) {
                return db.define(tbName, {
                    uid: {type: 'text', key: true},
                    username : String,
                    password : String
                },{
                    id : 'uid'
                });
            },
            emlog_options: function (db, tbName) {
                return db.define(tbName, {
                    option_id: {type: 'text', key: true},
                    option_name: String,
                    option_value: String
                },{
                    id : 'option_id'
                });
            }
        }
    }()*/
};
module.exports = lybyDB;